import { excludesRouterName, tokenName } from '@/server/base'
import router from '@/store/modules/router'
import { defineAsyncComponent } from 'vue'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'index',
    redirect: '/main/home'
  },
  /**
   * 主界面
   */
  {
    path: '/main',
    name: 'main',
    component: () => import('@/views/main/index.vue'),
    children: [
      //home
      {
        path: 'home/:reload?',
        name: 'home',
        component: () => import('@/views/main/home/index.vue'),
        meta: {
          zIndex: 1
        }
      },
      {
        path: 'task',
        name: 'task',
        component: () => import('@/views/main/task/index.vue'),
        meta: {
          zIndex: 2
        }
      },
      {
        path: 'performance',
        name: 'performance',
        component: () => import('@/views/main/performance/index.vue'),
        meta: {
          zIndex: 3
        }
      },
      {
        path: 'profile',
        name: 'profile',
        component: () => import('@/views/main/profile/index.vue'),
        meta: {
          zIndex: 4
        }
      }
    ]
  },

  /**
   * 二级页面
   * 二级页面的zIndex 最小值以20开始
   */
  {
    path: '/expand',
    name: 'expand',
    component: () => import('@/views/expand/index.vue'),
    meta: {
      zIndex: 20
    },
    children: [
      // 已完成
      {
        path: 'completed',
        name: 'completed',
        component: () => import('@/views/expand/completed/index.vue'),
        meta: {
          zIndex: 21
        }
      },
      // 已完成详情
      {
        path: 'completed-detail/:value',
        name: 'completed-detail',
        component: () => import('@/views/expand/completed/detail.vue'),
        meta: {
          zIndex: 22
        }
      },

      //已取消
      {
        path: 'cancel',
        name: 'cancel',
        component: () => import('@/views/expand/cancel/index.vue'),
        meta: {
          zIndex: 23
        }
      },
      //已取消详情
      {
        path: 'cancel-detail/:value',
        name: 'cancel-detail',
        component: () => import('@/views/expand/cancel/detail.vue'),
        meta: {
          zIndex: 24
        }
      },
      
      
      //进行中
      {
        path: 'going/:formType?',
        name: 'going',
        component: () => import('@/views/expand/going/index.vue'),
        meta: {
          zIndex: 25
        }
      },
      //进行中详情
      {
        path: 'going-detail/:value/:formType?',
        name: 'going-detail',
        component: () => import('@/views/expand/going/detail.vue'),
        meta: {
          zIndex: 26
        }
      },
       


      /**
       * 待接單
       */
      {
        path: 'wait',
        name: 'wait',
        component: () => import('@/views/expand/wait/index.vue'),
        meta: {
          zIndex: 27
        }
      },
      //接單详情
      {
        path: 'wait-detail/:value?',
        name: 'wait-detail',
        component: () => import('@/views/expand/wait/detail.vue'),
        meta: {
          zIndex: 28
        }
      },
      //取消或申请转交工单

      {
        path: 'wait-detail-reject/:type/:value?',
        name: 'wait-detail-reject',
        component: () => import('@/views/expand/wait/detail-reject.vue'),
        meta: {
          zIndex: 29
        }
      },

      /**
       * 用户个人设置
       */
      {
        path: 'profile-setting',
        name: 'profile-setting',
        component: () => import('@/views/main/profile/setting.vue'),
        meta: {
          zIndex: 30
        }
      },

      /**
       * 点检系统外链
       */

      {
        path: 'profile-spot-check',
        name: 'profile-spot-check',
        component: () => import('@/views/main/profile/spot-check.vue'),
        meta: {
          zIndex: 31
        }
      },

      /**
       * 欢迎登录页面
       */
      {
        path: 'welcome',
        name: 'welcome',
        component: () => import('@/views/expand/welcome/index.vue'),
        meta: {
          zIndex: 32
        }
      },
      {
        path: 'login',
        name: 'login',
        component: () => import('@/views/expand/login/index.vue'),
        meta: {
          zIndex: 33
        }
      },
      {
        path: 'register',
        name: 'register',
        component: () => import('@/views/expand/register/index.vue'),
        meta: {
          zIndex: 34
        }
      }
    ]
  },

  /**
   * 异常地址
   */
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    meta: {
      zIndex: 0
    }
  },
  // 404 page must be placed at the end !!!
  { path: '/:cathAll(.*)', redirect: '/404' }
]

const vueRouter = createRouter({
  history: createWebHashHistory(),
  routes
})

vueRouter.beforeEach((to: any, from: any, next: Function) => {
  if (!excludesRouterName.includes(to.name)) {
    const tokenString = localStorage.getItem(tokenName)
    if (!tokenString) {
      next({ name: 'welcome', params: { redirect: to.name } })
      return false
    }
    const userInfo = JSON.parse(tokenString)
    if (!userInfo) {
      next({ name: 'welcome', params: { redirect: to.name } })
      return false
    }
  }
  next()
  return true
})



export const pathRouter = routes

export default vueRouter
